
import { } from 'game/utils';
import { Creep, StructureSpawn } from 'game/prototypes';
import { } from 'game/constants';
import { CreepManager } from './CreepManager';

// 单位管理
export class SpawnHander {

    /**
     * @type {StructureSpawn}
     */
    spawn;
    /**
     * @type {CreepManager}
     */
    creepManager;

    loop() {
        // 保持工作单位满员
        if (!this.creepManager.wordCreepIsFull()) {
            this.doSpawnWorkCreep();
        } else {
            // 生产战斗单位
            this.doSpawnMelleCreep();
        }
    }

    // 生产搬运单位
    doSpawnWorkCreep() {
        try {
            var creep = this.spawn.spawnCreep(this.creepManager.WORK_BODY).object;
            if (typeof (creep) == 'object') {
                console.log("生产工作单位");
                this.creepManager.workCreeps.push(new CreepUnit(creep));
            }
        } catch (ex) {
            console.log("doSpawnCreep:" + ex);
        }
    }

    // 生产战斗单位
    doSpawnMelleCreep() {
        try {
            var creep = this.spawn.spawnCreep(this.creepManager.MELEE_BODY).object;
            if (typeof (creep) == 'object') {
                console.log("生产战斗单位");
                this.creepManager.melleCreeps.push(new CreepUnit(creep));
            }
        } catch (ex) {
            console.log("doSpawnCreep:" + ex);
        }
    }

}
// 0 = 初始化，1 = 活着，-1 = 死亡
export const CREEP_LEFT_STATUS_DIE = -1;
export const CREEP_LIFT_STATUS_INIT = 0;
export const CREEP_LIFT_STATUS_LIVING = 1;

export class CreepUnit {
    // 状态  
    _liftStatus = CREEP_LIFT_STATUS_INIT;
    creep;

    /**
     * @param {Creep} creep
     */
    constructor(creep) {
        this.creep = creep;
    }

    getLiftStatus() {
        if(this.creep.exists == true) {
            return CREEP_LIFT_STATUS_LIVING;
        }
        if(this.creep.id)  {
            return CREEP_LEFT_STATUS_DIE;
        }else{
            return CREEP_LIFT_STATUS_INIT;
        }
    }
}